import {AbstractControl, ValidationErrors, ValidatorFn, Validators} from '@angular/forms';
import { NzSafeAny } from 'ng-zorro-antd/core/types';

export type MyErrorsOptions = { 'zh-cn': string; en: string } & Record<string, NzSafeAny>;
export type MyValidationErrors = Record<string, MyErrorsOptions>;

export class MyValidators extends Validators {
  static minLength(minLength: number): ValidatorFn {
    return (control: AbstractControl): MyValidationErrors | null => {
      if (Validators.minLength(minLength)(control) === null) {
        return null;
      }
      return { minlength: { 'zh-cn': `最小长度为 ${minLength}`, en: `MinLength is ${minLength}` } };
    };
  }

  static maxLength(maxLength: number): ValidatorFn {
    return (control: AbstractControl): MyValidationErrors | null => {
      if (Validators.maxLength(maxLength)(control) === null) {
        return null;
      }
      return { maxlength: { 'zh-cn': `最大长度为 ${maxLength}`, en: `MaxLength is ${maxLength}` } };
    };
  }

  static dateRange(control: AbstractControl): MyValidationErrors | null {
    const value = control.value;
    
    if (isEmptyInputValue(value)) {
      return null;
    }

    if(value[0].getTime()>=value[1].getTime()){
      return { dateRange: { 'zh-cn': `起始时间不能大于结束时间`, en: `The start time cannot be greater than the end time` } }
    }else{
      return null;
    }
  }

  static inputNumber(int:number,float: number): ValidatorFn {
    return  (control: AbstractControl): MyValidationErrors | null => {
      const value = control.value;
      if(!value){
        return null
      }
      const reg=new RegExp(/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/)//数字和小数点正则验证
      if (reg.test(value)) {
        if(value.indexOf('.')!==-1){//小数
          if(value.split('.')[1]&&value.split('.')[1].length>float){
            return { inputNumber: { 'zh-cn': `只能输入${float}位小数`, en: `The start time cannot be greater than the end time` } }
          }else if(value.split('.')[0]&&value.split('.')[0].length>int){
            return { inputNumber: { 'zh-cn': `只能输入${int}位整数`, en: `The start time cannot be greater than the end time` } }
          }else{
            return null
          }
        }else{//正整数
          if(value.length>int){
            return { inputNumber: { 'zh-cn': `只能输入${int}位整数`, en: `The start time cannot be greater than the end time` } }
          }else{
            return null
          }
        }
      }else{
        return { inputNumber: { 'zh-cn': `只能输入正整数和小数`, en: `The start time cannot be greater than the end time` } }
      }
    } 
  }

  static mobile(control: AbstractControl): MyValidationErrors | null {
    const value = control.value;

    if (isEmptyInputValue(value)) {
      return null;
    }

    return isMobile(value)
      ? null
      : { mobile: { 'zh-cn': `手机号码格式不正确`, en: `Mobile phone number is not valid` } };
  }
}

function isEmptyInputValue(value: NzSafeAny): boolean {
  return value == null || value.length === 0;
}

function isMobile(value: string): boolean {
  return typeof value === 'string' && /(^1\d{10}$)/.test(value);
}
export function needNameValidator(nameRe: RegExp): ValidatorFn {
    return (control: AbstractControl): ValidationErrors | null => {
        if(nameRe.test(control.value)){
            return null
        }
        return { errValue:  control.value}
    };
}

export function judgeDate(): ValidatorFn{
    return  (control: AbstractControl): ValidationErrors | null => {
        try {
            let d = new Date(control.value);
            return null
        } catch (err){
            return { errValue: control.value}
        }

    };
}

export const requiredWithTrim = (control: AbstractControl): ValidationErrors | null => {
    if ((control.value ?? '').toString().trim()) {
        return null;
    }
    return {errValue: ''};
};

export function checkIdcard(idcard) {
  let Errors = ["",
      "身份证号码位数不对!",
      "身份证号码出生日期超出范围或含有非法字符!",
      "身份证号码校验错误!",
      "身份证地区非法!",
      "请输入身份证号码!"];
  let area = {
      11: "北京",
      12: "天津",
      13: "河北",
      14: "山西",
      15: "内蒙古",
      21: "辽宁",
      22: "吉林",
      23: "黑龙江",
      31: "上海",
      32: "江苏",
      33: "浙江",
      34: "安徽",
      35: "福建",
      36: "江西",
      37: "山东",
      41: "河南",
      42: "湖北",
      43: "湖南",
      44: "广东",
      45: "广西",
      46: "海南",
      50: "重庆",
      51: "四川",
      52: "贵州",
      53: "云南",
      54: "西藏",
      61: "陕西",
      62: "甘肃",
      63: "青海",
      64: "宁夏",
      65: "新疆",
      71: "台湾",
      81: "香港",
      82: "澳门",
      91: "国外"
  }
  if(!idcard)
      return Errors[5];

  let Y, JYM;
  let S, M, ereg;
  let idcard_array;
  idcard_array = idcard.split("");
  //地区检验
  if (area[parseInt(idcard.substr(0, 2))] == null)
      return Errors[4];
  //身份号码位数及格式检验
  switch (idcard.length) {
      case 15:
          if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 === 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 === 0 && (parseInt(idcard.substr(6, 2)) + 1900) % 4 === 0)) {
              ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/; //测试出生日期的合法性
          } else {
              ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/; //测试出生日期的合法性
          }
          if (ereg.test(idcard))
              return Errors[0];
          else
              return Errors[2];
      case 18:
          //18位身份号码检测
          //出生日期的合法性检查
          //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
          //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
          if (parseInt(idcard.substr(6, 4)) % 4 === 0 || (parseInt(idcard.substr(6, 4)) % 100 === 0 && parseInt(idcard.substr(6, 4)) % 4 === 0)) {
              ereg = /^[1-9][0-9]{5}(1|2)[0-9]{3}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/; //闰年出生日期的合法性正则表达式
          } else {
              ereg = /^[1-9][0-9]{5}(1|2)[0-9]{3}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/; //平年出生日期的合法性正则表达式
          }
          if (ereg.test(idcard)) { //测试出生日期的合法性
            return Errors[0];  //不验证校验位直接返回正确
              //计算校验位
              // S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3;
              // Y = S % 11;
              // M = "F";
              // JYM = "10X98765432";
              // M = JYM.substr(Y, 1); //判断校验位
              // if (M === idcard_array[17])
              //     return Errors[0]; //检测ID的校验位
              // else
              //     return Errors[3];
          } else {
              return Errors[2];
          }
      default:
          return Errors[1];
  }
}

